//
//  AppDelegate.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/1/21.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    var observer : NSObjectProtocol?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        //初始化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.grayColor()
        window?.rootViewController = HMMainViewController()
        window?.makeKeyAndVisible()
        observer = NSNotificationCenter.defaultCenter().addObserverForName(HMSwitchRootVcNotification, object: nil, queue: nil) { [weak self](noti) -> Void in
            if noti.object is HMWelcomeViewController {
                self?.window?.rootViewController = HMMainViewController()
                
            }else{
               self?.window?.rootViewController = HMWelcomeViewController()
            }
            
        }
        return true
    }

    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(observer!)
    }
    
    // 默认根控制器
    func defaultRootViewController() -> UIViewController {
        
        if hasNewVersion() {
            return HMNewFeatureViewController()
        }else {
            return HMWelcomeViewController()
        }
    }
    
    func hasNewVersion() -> Bool {
        
        // 沙盒里面取
        let sanVersion = NSUserDefaults.standardUserDefaults().stringForKey("version")
        
        // 取出当前的
        let currentVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String
        
        // 保存当前到沙盒
        NSUserDefaults.standardUserDefaults().setObject(currentVersion!, forKey: "version")
        
        // 进行对比
        // 如果沙盒里面的版本号不小于当前版本号，就代表没有新版本
        if let san = sanVersion where san.compare(currentVersion!) != NSComparisonResult.OrderedAscending {
            return false
        }
        
        return true
    }




}

